//
//  SwiftUIViewController.swift
//  TestIos
//
//  Created by 李亚东 on 2025/11/5.
//  Copyright © 2025 Liyadong. All rights reserved.
//

import UIKit
import SnapKit

@available(iOS 13.0, *)
@objc(SwiftUIViewController)
class SwiftUIViewController: UIViewController {
    
    private var sortBtnView: NHTSorBtnView!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    private func setupUI() {
        view.backgroundColor = .systemBackground
        
        // 创建下拉菜单按钮视图
        sortBtnView = NHTSorBtnView()
        sortBtnView.title = "排序"
        sortBtnView.backgroundColor = .secondarySystemBackground
        sortBtnView.layer.cornerRadius = 8
        
        // 设置点击回调
        sortBtnView.onTap = { [weak self] in
            guard let self = self else { return }
            print("下拉菜单按钮被点击了")
            // 在这里可以处理展开/收起菜单的逻辑
        }
        
        // 添加到视图
        view.addSubview(sortBtnView)
        
        // 设置约束
        sortBtnView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide).offset(50)
            make.height.equalTo(50)
            make.width.equalTo(100)
        }
        
        // 示例：创建第二个按钮
        let secondSortBtnView = NHTSorBtnView()
        secondSortBtnView.title = "筛选"
        secondSortBtnView.backgroundColor = .secondarySystemBackground
        secondSortBtnView.layer.cornerRadius = 8
        secondSortBtnView.onTap = {
            print("第二个下拉菜单按钮被点击了")
        }
        
        view.addSubview(secondSortBtnView)
        
        secondSortBtnView.snp.makeConstraints { make in
            make.top.equalTo(sortBtnView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
            make.height.equalTo(50)
        }
        
        // 示例：外部设置 isExpanded 属性（会触发动画）
        // 创建一个按钮来演示外部设置
        let toggleButton = UIButton(type: .system)
        toggleButton.setTitle("外部切换第一个按钮状态", for: .normal)
        toggleButton.backgroundColor = .systemBlue
        toggleButton.setTitleColor(.white, for: .normal)
        toggleButton.layer.cornerRadius = 8
        toggleButton.addTarget(self, action: #selector(toggleFirstButton), for: .touchUpInside)
        view.addSubview(toggleButton)
        
        toggleButton.snp.makeConstraints { make in
            make.top.equalTo(secondSortBtnView.snp.bottom).offset(30)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(44)
        }
    }
    
    @objc private func toggleFirstButton() {
        // 外部设置 isExpanded 属性，会自动触发旋转动画
        sortBtnView.isExpanded.toggle()
        print("外部设置 isExpanded = \(sortBtnView.isExpanded)")
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
